package com.morder.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.morder.Info;
import com.morder.R;
import com.morder.application.BuildConfig;
import com.morder.util.BitmapManager;

public class InfoAdapter extends BaseAdapter {
  private ArrayList<Info> mArrayList;
  private Context mContext;
  private BitmapManager bmpManager;

  public InfoAdapter(ArrayList<Info> list, Context context) {
    this.mArrayList = list;
    this.mContext = context;
    this.bmpManager = new BitmapManager(BitmapFactory.decodeResource(context.getResources(), R.drawable.widget_dface_loading));
  }

  @Override
  public int getCount() {
    return mArrayList.size();
  }

  @Override
  public Object getItem(int position) {
    return mArrayList.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  /**
   * 插入
   * 
   * @param list
   */
  public void insertData(List<Info> list) {
    this.mArrayList.addAll(0, list);
    this.notifyDataSetChanged();
  }
  
  public List<Info> getData(){
    return mArrayList;
  }
  
  public void addMoreData(List<Info> list) {
    this.mArrayList.addAll(list);
    this.notifyDataSetChanged();
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
      convertView = LayoutInflater.from(mContext.getApplicationContext()).inflate(R.layout.info_list_item, null);
      holder = new ViewHolder();
      holder.value = (TextView) convertView.findViewById(R.id.value);
      holder.value.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
      holder.image = (ImageView) convertView.findViewById(R.id.image);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    String picUrl = mArrayList.get(position).getImage();
    String picName = picUrl.substring(picUrl.lastIndexOf("/") + 1, picUrl.length());
    final String cover = BuildConfig.DREADER_DATA_ICON_PATH + "/" + picName;
    System.out.println("cover: " + cover);
    final String picPath = BuildConfig.DREADER_DATA_ICON_PATH + "/" + picName;
    final File picFile = new File(picPath);
    System.out.println("picFile.exists(): " + picFile.exists());
    if (!picFile.exists()) {
//      loadImage(holder.image, picUrl);
      bmpManager.loadBitmap(picUrl, holder.image);
    } else {
      Bitmap bm = BitmapFactory.decodeFile(cover);
      if (bm != null) {
        holder.image.setImageBitmap(bm);
      }
    }

    holder.value.setText("￥" + mArrayList.get(position).getValue());
    return convertView;
  }

  class ViewHolder {
    TextView value;
    ImageView image;
  }
}
